﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Chefbook.ViewModels;
using Chefbook.DataAccess;
using UnitTests.Properties;

namespace UnitTests.ViewModelsTests
{
    [TestFixture]
    public class CategoryTreeViewModelFixture
    {
        private CategoryTreeViewModel viewmodel;
        ChefbookXmlRepository repo;
        List<CategoryViewModel> allCategories;

        [SetUp]
        public void Init()
        {
            repo = new ChefbookXmlRepository(Resources.RecipeXmlPath, Resources.CategoryXmlPath);
            viewmodel = new CategoryTreeViewModel(repo.RootCategories);
            allCategories = repo.Categories.Values.ToList();
        }

        [Test]
        public void SelectedCategoryPropertyChangedTest()
        {
            var mycategory = allCategories[3];
            var mycategory2 = allCategories[4];
            Assert.False(mycategory.IsSelected);
            viewmodel.AssertPropertyChangedFired(() =>
                viewmodel.SelectedCategory = mycategory, "SelectedCategory");
            Assert.True(mycategory.IsSelected);
            viewmodel.AssertPropertyChangedFired(() =>
                viewmodel.SelectedCategory = mycategory);
            Assert.True(mycategory.IsSelected);
            viewmodel.AssertPropertyChangedFired(() =>
                viewmodel.SelectedCategory = mycategory2, "SelectedCategory");
            Assert.False(mycategory.IsSelected);
            Assert.True(mycategory2.IsSelected);
        }

        [Test]
        public void CategoryIsSelectedChangesSelectedCategory()
        {
            var mycategory = allCategories[3];
            viewmodel.AssertPropertyChangedFired(() =>
                mycategory.IsSelected = true, "SelectedCategory");
            Assert.AreEqual(mycategory, viewmodel.SelectedCategory);
        }

        [Test]
        public void MarkSecondCategorySelectedAndTheFirstUnselectedChangesSelectedCategory()
        {
            var mycategory1 = allCategories[3];
            var mycategory2 = allCategories[2];
            viewmodel.AssertPropertyChangedFired(() =>
                mycategory1.IsSelected = true, "SelectedCategory");
            Assert.AreEqual(mycategory1, viewmodel.SelectedCategory);
            Assert.True(mycategory1.IsSelected);
            Assert.False(mycategory2.IsSelected);
            viewmodel.AssertPropertyChangedFired(() =>
                mycategory2.IsSelected = true, "SelectedCategory");
            Assert.AreEqual(mycategory2, viewmodel.SelectedCategory);
            Assert.True(mycategory2.IsSelected);
            Assert.False(mycategory1.IsSelected);
        }
    }
}
